﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http;
using System.Web.Security;

namespace PNProject.Controllers
{
    [Authorize]
    public class MemberAPIController : ApiController
    {
        //
        // GET: /MemberAPI/

        [HttpGet]
        public HttpResponseMessage ChangePassword(string pw_c, string pw_n, string pw_r)
        {
            if ((pw_n != pw_r) || pw_c.Length < 6 || pw_n.Length < 6)
            {
                return new HttpResponseMessage()
                {
                    Content = new StringContent("no")
                };
            }
            bool changePasswordSucceeded;
            try
            {
                MembershipUser currentUser = Membership.GetUser(User.Identity.Name, true /* userIsOnline */);
                changePasswordSucceeded = currentUser.ChangePassword(pw_c, pw_n);
                if (changePasswordSucceeded)
                {
                    return new HttpResponseMessage()
                    {
                        Content = new StringContent("ok")
                    };
                }
                else
                {
                    return new HttpResponseMessage()
                    {
                        Content = new StringContent("no")
                    };
                }
            }
            catch (Exception)
            {
                changePasswordSucceeded = false;
                return new HttpResponseMessage()
                {
                    Content = new StringContent("no")
                };
            }
            
        }

    }
}
